package com.dstz.bpm.api.engine.action.cmd;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dstz.base.api.exception.BusinessException;
import com.dstz.base.core.util.AppUtil;
import com.dstz.base.core.util.StringUtil;
import com.dstz.bpm.api.constant.ActionType;
import com.dstz.bpm.api.engine.action.handler.ActionHandler;
import com.dstz.bpm.api.engine.context.BpmContext;
import com.dstz.bpm.api.exception.BpmStatusCode;
import com.dstz.bpm.api.model.def.IBpmDefinition;
import com.dstz.bpm.api.model.inst.BpmExecutionStack;
import com.dstz.bpm.api.model.inst.IBpmInstance;
import com.dstz.bus.api.model.IBusinessData;
import com.dstz.form.api.model.FormCategory;
import com.dstz.org.api.model.IUser;
import com.dstz.org.api.service.UserService;
import com.dstz.sys.api.model.SysIdentity;
import com.dstz.sys.util.ContextUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class BaseActionCmd implements ActionCmd {
   protected Map<String, Object> variable_ = new HashMap();
   protected Map<String, List<SysIdentity>> identityMap_ = new HashMap();
   protected JSONObject busData;
   protected IBpmDefinition bpmDefinition = null;
   protected String defId;
   protected IBpmInstance bpmInstance = null;
   protected String instanceId;
   protected Map<String, IBusinessData> bizDataMap = new HashMap();
   private String actionName;
   private String businessKey;
   private String dataMode;
   private String opinion;
   protected String destination;
   protected String formId;
   protected boolean isSource = false;
   private BpmExecutionStack executionStack;
   protected JSONObject extendConf;
   protected boolean hasExecuted = false;

   public abstract void initSpecialParam(FlowRequestParam var1);

   public abstract String getNodeId();

   public BaseActionCmd() {
   }

   public BaseActionCmd(FlowRequestParam flowParam) {
      this.isSource = true;
      this.setActionName(flowParam.getAction());
      this.setDefId(flowParam.getDefId());
      this.setInstanceId(flowParam.getInstanceId());
      this.setBusinessKey(flowParam.getBusinessKey());
      this.initSpecialParam(flowParam);
      if (CollectionUtil.isNotEmpty(flowParam.getNodeUsers())) {
         this.handleUserSetting(flowParam.getNodeUsers());
      }

      this.setBusData(flowParam.getData());
      String formType = FormCategory.INNER.value();
      if (StringUtil.isNotEmpty(flowParam.getFormType())) {
         formType = flowParam.getFormType();
      }

      if (FormCategory.INNER.value().equals(formType)) {
         this.setDataMode("bo");
      } else {
         this.setDataMode("pk");
      }

      this.setExtendConf(flowParam.getExtendConf());
      this.setOpinion(flowParam.getOpinion());
   }

   private void handleUserSetting(JSONObject jsonObject) {
      if (!jsonObject.isEmpty()) {
         Map<String, List<SysIdentity>> map = new HashMap();
         Set<String> nodeIds = jsonObject.keySet();
         Iterator var4 = nodeIds.iterator();

         while(true) {
            String nodeId;
            JSONArray users;
            do {
               do {
                  if (!var4.hasNext()) {
                     this.setBpmIdentities(map);
                     return;
                  }

                  nodeId = (String)var4.next();
                  users = jsonObject.getJSONArray(nodeId);
               } while(users == null);
            } while(users.isEmpty());

            List<SysIdentity> userList = new ArrayList();
            Iterator var8 = users.iterator();

            while(var8.hasNext()) {
               Object userObj = var8.next();
               JSONObject user = (JSONObject)userObj;
               SysIdentity bpmInentity = (SysIdentity)JSON.toJavaObject(user, SysIdentity.class);
               if (!StringUtil.isEmpty(bpmInentity.getId())) {
                  userList.add(bpmInentity);
               }
            }

            map.put(nodeId, userList);
         }
      }
   }

   public Map<String, Object> getActionVariables() {
      return this.variable_;
   }

   public void setActionVariables(Map<String, Object> variables) {
      this.variable_ = variables;
   }

   public void setBpmIdentities(Map<String, List<SysIdentity>> map) {
      this.identityMap_ = map;
   }

   public void clearBpmIdentities() {
      this.identityMap_.clear();
   }

   public void addBpmIdentity(String key, SysIdentity bpmIdentity) {
      List<SysIdentity> list = (List)this.identityMap_.get(key);
      if (CollectionUtil.isEmpty(list)) {
         list = new ArrayList();
         list.add(bpmIdentity);
         this.identityMap_.put(key, list);
      } else {
         list.add(bpmIdentity);
      }

   }

   public void addBpmIdentity(String key, List<SysIdentity> bpmIdentityList) {
      List<SysIdentity> list = (List)this.identityMap_.get(key);
      if (CollectionUtil.isEmpty(list)) {
         list = new ArrayList();
         list.addAll(bpmIdentityList);
         this.identityMap_.put(key, list);
      } else {
         list.addAll(bpmIdentityList);
      }

   }

   public void setBpmIdentity(String key, List<SysIdentity> bpmIdentityList) {
      List<SysIdentity> list = (List)this.identityMap_.get(key);
      if (CollectionUtil.isEmpty(list)) {
         list = new ArrayList();
         list.addAll(bpmIdentityList);
         this.identityMap_.put(key, list);
      } else {
         list.clear();
         list.addAll(bpmIdentityList);
      }

   }

   public List<SysIdentity> getBpmIdentity(String nodeId) {
      return (List)this.identityMap_.get(nodeId);
   }

   public Map<String, List<SysIdentity>> getBpmIdentities() {
      return this.identityMap_;
   }

   public boolean isSource() {
      return this.isSource;
   }

   public void setSource(boolean isSource) {
      this.isSource = isSource;
   }

   public JSONObject getBusData() {
      return this.busData;
   }

   public void setBusData(JSONObject busData) {
      this.busData = busData;
   }

   public IBpmInstance getBpmInstance() {
      return this.bpmInstance;
   }

   public void setBpmInstance(IBpmInstance bpmInstance) {
      this.bpmInstance = bpmInstance;
   }

   public String getDataMode() {
      return this.dataMode;
   }

   public void setDataMode(String mode) {
      this.dataMode = mode;
   }

   public String getBusinessKey() {
      return this.businessKey;
   }

   public String getFormId() {
      return this.formId;
   }

   public BpmExecutionStack getExecutionStack() {
      return this.executionStack;
   }

   public void setExecutionStack(BpmExecutionStack executionStack) {
      this.executionStack = executionStack;
   }

   public String getInstanceId() {
      return StringUtil.isEmpty(this.instanceId) && this.bpmInstance != null ? this.bpmInstance.getId() : this.instanceId;
   }

   public void setInstanceId(String instanceId) {
      this.instanceId = instanceId;
   }

   public String getDefId() {
      return StringUtil.isEmpty(this.defId) && this.bpmInstance != null ? this.bpmInstance.getDefId() : this.defId;
   }

   public JSONObject getExtendConf() {
      return this.extendConf;
   }

   public void setExtendConf(JSONObject extendConf) {
      this.extendConf = extendConf;
   }

   public void setDefId(String defId) {
      this.defId = defId;
   }

   public String getOpinion() {
      return this.opinion;
   }

   public void setOpinion(String opinion) {
      this.opinion = opinion;
   }

   public Map<String, IBusinessData> getBizDataMap() {
      return this.bizDataMap;
   }

   public void setBizDataMap(Map<String, IBusinessData> bizDataMap) {
      this.bizDataMap = bizDataMap;
   }

   public void setFormId(String formId) {
      this.formId = formId;
   }

   public void setBusinessKey(String businessKey) {
      this.businessKey = businessKey;
   }

   public void setCurAccount(String curAccount) {
      UserService userService = (UserService)AppUtil.getBean(UserService.class);
      IUser user = userService.getUserByAccount(curAccount);
      ContextUtil.setCurrentUser(user);
   }

   public String getActionName() {
      return this.actionName;
   }

   public ActionType getActionType() {
      return ActionType.fromKey(this.getActionName());
   }

   public void setActionName(String actionName) {
      this.actionName = actionName;
   }

   public IBpmDefinition getBpmDefinition() {
      return this.bpmDefinition;
   }

   public void setBpmDefinition(IBpmDefinition bpmDefinition) {
      this.bpmDefinition = bpmDefinition;
   }

   public String getDestination() {
      return this.destination;
   }

   public void setDestination(String destination) {
      this.destination = destination;
   }

   @Override
   public synchronized String executeCmd() {
      if (this.hasExecuted) {
         throw new BusinessException("action cmd caonot be invoked twice", BpmStatusCode.NO_PERMISSION);
      } else {
         this.hasExecuted = true;
         ActionType actonType = ActionType.fromKey(this.getActionName());
         ActionHandler handler = (ActionHandler)AppUtil.getBean(actonType.getBeanId());
         if (handler == null) {
            throw new BusinessException("action beanId cannot be found :" + actonType.getName(), BpmStatusCode.NO_TASK_ACTION);
         } else {
            BpmContext.cleanTread();
            handler.execute(this);
            return handler.getActionType().getName();
         }
      }
   }
}
